package com.sanuo.pico.act;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.MenuProvider;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;

import com.menu.app.CrashHandler;
import com.menu.app.activity.BaseBoardActivity;
import com.menu.app.bean.Command;
import com.menu.app.model.Models;
import com.menu.app.util.OnMultiTouchListener;
import com.sanuo.pico.App;
import com.sanuo.pico.BuildConfig;
import com.sanuo.pico.R;
import com.sanuo.pico.act.work.WarnAct;
import com.sanuo.pico.event.Event;
import com.sanuo.pico.utils.Const;
import com.sanuo.pico.utils.MySerialHandler;
import com.sanuo.pico.utils.PortPipe;

import java.util.HashMap;

import me.drakeet.support.toast.ToastUtil;

public class BoardActivity extends BaseBoardActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding.camera.setVisibility(View.INVISIBLE);
        View view = new View(this);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(200, 200);
        params.gravity = Gravity.BOTTOM;
        binding.frame.addView(view, params);
        view.setOnTouchListener(new OnMultiTouchListener() {
            @Override
            public void onMultiTouch(View v, MotionEvent event, int touchCount) {
                if (!BuildConfig.DEBUG) {
                    return;
                }
                if (touchCount == 5) {
                    CrashHandler.triggerRebirth(App.getApp());
                }
            }
        });
    }

    @Override
    protected boolean openWork() {
        if (!Const.isAbove1315()) {
            ToastUtil.showShortToast(this, R.string.update_board);
            return true;
        }
//        if (!Event.connect.isConnect()) {
//            ToastUtil.showShortToast(this, R.string.no_handle);
//            return true;
//        }
        App.getApp().setIgnore(false);
        com.sanuo.pico.utils.Command.getHandleType();

        h.postDelayed(() -> {
            com.sanuo.pico.utils.Command.sendTouchEvent(0, 0x11);
        }, 100);
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        onMessageEvent(Models.Companion.get().getPointLoadingEvent());
        PortPipe.getInstance().goBack(null);
    }

    final Runnable checkRunnable = new Runnable() {
        @Override
        public void run() {
            checkState(true);
        }
    };


    @Override
    protected void checkState(boolean jump) {
        if (!jump) {
            return;
        }
        if (!Models.Companion.get().getStartEvent().getValue()) {
            if (MySerialHandler.getData) {
                showLoading(true);
                h.removeCallbacks(checkRunnable);
                h.postDelayed(checkRunnable, 500);
                return;
            } else {
                ToastUtil.showShortToast(this, com.menu.app.R.string.disconnect);
            }
            if (!Const.jump) {
                return;
            }
        }
        if (Event.warn.hasWarn()) {
            Intent i = new Intent(this, WarnAct.class);
            startActivity(i);
            return;
        }
        toWork();
    }

    @Override
    protected boolean checkHandle(Runnable runnable) {
        return true;
    }

    @Override
    protected void doUpdateRent(Command bean, Runnable runnable) {
        super.doUpdateRent(bean, runnable);
//
//        hideLoading();
//        h.post(() -> runnable.run());

//        App.getApp().setIgnore(false);
////        sendTouchEvent((byte) 0, (byte) 3);
//        sendTouchEvent((byte) 0, (byte) 0x92);
    }

    @Override
    public void addMenuProvider(@NonNull MenuProvider provider, @NonNull LifecycleOwner owner, @NonNull Lifecycle.State state) {

    }

    @Override
    protected HashMap convertCustomData(HashMap map) {
        if (map == null) {
            return null;
        }
        map.remove("point");
        map.remove("part");

        return map;
    }

    @Override
    protected String getReportUrl() {
        return Const.REPORT;
    }
}
